package com.example.teacher.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.commonutils.ResultData;
import com.example.servicebase.exceptionhandler.CustomException;
import com.example.teacher.entity.Teacher;
import com.example.teacher.entity.vo.TeacherQuery;
import com.example.teacher.service.TeacherService;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-05-21
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/backstage/teacher")
public class TeacherController {
    @Autowired
    private TeacherService service;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping(value = "findAll",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData list(){
        return ResultData.ok().data("items",service.list(null));
    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping(value = "{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData removeTeacher(@PathVariable String id){
        if (!StringUtil.isNullOrEmpty(id)){
            service.removeById(id);
            return ResultData.ok();
        }else {
            return ResultData.error();
        }
    }

    @ApiOperation(value = "分页讲师列表")
    @GetMapping(value = "{page}/{limit}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)//page当前页,limit每页显示条数
    public ResultData pageList(
        @ApiParam(name = "page", value = "当前页码", required = true)
        @PathVariable Long page,
        @ApiParam(name = "limit", value = "每页记录数", required = true)
        @PathVariable Long limit){
        Page<Teacher> pageParam = new Page<>(page, limit);
        service.page(pageParam, null);//mp分页插件的分页方法
        List<Teacher> records = pageParam.getRecords();//记录
        long total = pageParam.getTotal();//总记录数
        return ResultData.ok().data("total", total).data("rows", records);
    }

    //条件查询带分页
    @ApiOperation(value = "分页条件查询讲师列表")
    @PostMapping(value = "pageTeacherCondition/{page}/{limit}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData pageTeacherCondition(
            @PathVariable("page") long page,
            @PathVariable("limit") long limit,
            @RequestBody(required = false) TeacherQuery teacherQuery
    ){
        Page<Teacher> pageTeacher = new Page<>(page,limit);
        //构建条件
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        //多条件组合查询
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        //根据创建事件排序
        wrapper.orderByDesc("gmt_create");
        //分页查询
        service.page(pageTeacher,wrapper);
        List<Teacher> records = pageTeacher.getRecords();//记录
        long total = pageTeacher.getTotal();//总记录数
        return ResultData.ok().data("total", total).data("rows", records);
    }

    //添加讲师
    @ApiOperation(value = "添加讲师")
    @PostMapping(value = "addTeacher",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData addTeacher(@RequestBody Teacher teacher){
        String name = teacher.getName();
        if (!StringUtil.isNullOrEmpty(name)){
            throw new CustomException(20001,"请填写相关信息");
        }
        boolean save = service.save(teacher);
        if (save){
            return ResultData.ok();
        }else {
            return ResultData.error();
        }
    }

    //根据id查询教师
    @GetMapping(value = "/getTeacherById/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData getTeacher(@PathVariable String id){
        Teacher teacher = service.getById(id);
        return ResultData.ok().data("teacher",teacher);
    }

    //讲师修改功能
    @PutMapping(value = "updateTeacher",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData updateTeacher(@RequestBody Teacher teacher){
        boolean flag = service.updateById(teacher);
        if (flag){
            return ResultData.ok();
        }else{
            return ResultData.error();
        }
    }
}

